#include "devicesizetable.h"
#include "qprocess.h"
#include "qtableview.h"
#include "qheaderview.h"
#include "qfileinfo.h"
#include "qdir.h"
#include "qprogressbar.h"
#include "qtimer.h"
#include "qdebug.h"
#include "windows.h"

#define GB (1024*1024*1024)
#define MB (1024*1024)
#define KB (1024)


DeviceSizeTable::DeviceSizeTable(QWidget *parent) :QTableWidget(parent)
{
    bgColor = QColor(255, 255, 255);

    chunkColor1 = QColor(100, 184, 255);
    chunkColor2 = QColor(24, 189, 155);
    chunkColor3 = QColor(255, 107, 107);

    textColor1 = QColor(10, 10, 10);
    textColor2 = QColor(255, 255, 255);
    textColor3 = QColor(255, 255, 255);
}

DeviceSizeTable::~DeviceSizeTable()
{

}

QColor DeviceSizeTable::getBgColor() const
{
    return this->bgColor;
}

QColor DeviceSizeTable::getChunkColor1() const
{
    return this->chunkColor1;
}

QColor DeviceSizeTable::getTextColor1() const
{
    return this->textColor1;
}

void DeviceSizeTable::readData()
{

}

void DeviceSizeTable::loadDiskMonitor()
{
    this->clear();

    //设置列数和列宽
    this->setColumnCount(5);
    this->setColumnWidth(0,100);
    this->setColumnWidth(1,120);
    this->setColumnWidth(2,120);
    this->setColumnWidth(3,120);
    this->setColumnWidth(4,120);

    this->setStyleSheet("QTableWidget::item{padding:0px;}");

    QStringList headText;
    headText<<"盘符"<<"已用空间"<<"可用空间"<<"总大小"<<"已用百分比";

    this->setHorizontalHeaderLabels(headText);
    this->setSelectionBehavior(QAbstractItemView::SelectRows);
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    this->setSelectionMode(QAbstractItemView::SingleSelection);
    this->verticalHeader()->setVisible(true);
    this->horizontalHeader()->setStretchLastSection(true);
    QMetaObject::invokeMethod(this,"load",Qt::QueuedConnection);
}

void DeviceSizeTable::load()
{
    int row=this->rowCount();
    for(int i=0;i<row;i++)
    {
        this->removeRow(0);
    }

    QFileInfoList list=QDir::drives();
    foreach (QFileInfo dir, list) {
        QString dirName=dir.absoluteFilePath();
        LPCWSTR lpcwstrDriver=(LPCWSTR)dirName.utf16();
        ULARGE_INTEGER libFreeBytsAvaiblable,liTotalBytes,liTotalFreeBytes;
        if(GetDiskFreeSpaceEx(lpcwstrDriver,&libFreeBytsAvaiblable,&liTotalBytes,&liTotalFreeBytes))
        {
            QString use=QString::number((double)(liTotalBytes.QuadPart-liTotalFreeBytes.QuadPart)/GB,'f',1);
            use+="G";
            QString free=QString::number((double)(liTotalFreeBytes.QuadPart/GB,'f',1));
            free+="G";
            QString all = QString::number((double) liTotalBytes.QuadPart / GB, 'f', 1);
            all += "G";
            int percent=100-((double)liTotalFreeBytes.QuadPart/liTotalBytes.QuadPart)*100;
            insertSize(dirName,use,free,all,percent);
        }
    }
}

void DeviceSizeTable::loadUserMonitor()
{
    this->clear();
    int row=this->rowCount();
    for(int i=0;i<row;i++)
    {
        this->removeRow(0);
    }

    //设置列数和列宽
    this->setColumnCount(6);
    this->setColumnWidth(0,100);
    this->setColumnWidth(1,120);
    this->setColumnWidth(2,120);
    this->setColumnWidth(3,120);
    this->setColumnWidth(4,120);

    this->setStyleSheet("QTableWidget::item{padding:0px;}");

    QStringList headText;
    headText<<"姓名"<<"入职时间"<<"年龄"<<"部门"<<"学历"<<"职级";

    this->setHorizontalHeaderLabels(headText);

    row=this->rowCount();
    this->insertRow(row);

    QStringList valueText;
    valueText<<"李明"<<"2016-04-02"<<"35"<<"后勤部"<<"研究生"<<"中级";
    int colIndex=0;
    foreach (QString str, valueText) {
        QTableWidgetItem *item=new QTableWidgetItem(str);
        this->setItem(row,colIndex,item);
        colIndex++;
    }

}

void DeviceSizeTable::clearData()
{
    this->clear();
}

void DeviceSizeTable::setBgColor(const QColor &bgColor)
{
    if(this->bgColor!=bgColor)
    {
        this->bgColor=bgColor;
        this->load();
    }
}

void DeviceSizeTable::setChunkColor1(const QColor &chunkColor1)
{
    if(this->chunkColor1!=chunkColor1)
    {
        this->chunkColor1=chunkColor1;
        this->load();
    }
}

void DeviceSizeTable::setTextColor1(const QColor &textColor1)
{
    if(this->textColor1!=textColor1)
    {
        this->textColor1=textColor1;
        this->load();
    }
}

void DeviceSizeTable::insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent)
{
    int row=this->rowCount();
    this->insertRow(row);

    QTableWidgetItem *itemname=new QTableWidgetItem(name);
    QTableWidgetItem *itemuse=new QTableWidgetItem(use);
    QTableWidgetItem *itemfree=new QTableWidgetItem(free);
    QTableWidgetItem *itemall=new QTableWidgetItem(all);

    this->setItem(row,0,itemname);
    this->setItem(row,1,itemuse);
    this->setItem(row,2,itemfree);
    this->setItem(row,3,itemall);

    QProgressBar *bar=new QProgressBar();
    bar->setRange(0,100);
    bar->setValue(percent);


    QString qss = QString("QProgressBar{background:%1;border-width:0px;border-radius:0px;text-align:center;}"
                          "QProgressBar::chunk{border-radius:0px;}").arg(bgColor.name());

    if (percent < 50) {
        qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor1.name()).arg(chunkColor1.name());
    } else if (percent < 90) {
        qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor2.name()).arg(chunkColor2.name());
    } else {
        qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor3.name()).arg(chunkColor3.name());
    }

    bar->setStyleSheet(qss);
    this->setCellWidget(row,4,bar);
}

QSize DeviceSizeTable::sizeHint() const
{
    return QSize(500,300);
}

QSize DeviceSizeTable::minimumSizeHint() const
{
    return QSize(200,152);
}
